﻿using System.ServiceModel;
using NUnit.Framework;

namespace Universe.WCF.Behaviors.Test
{
    [TestFixture]
    public class TestAll
    {
        [Test]
        public void Test_Pure_Connection()
        {
            ServiceHost host = MyEnvionment.CreateService<IService1<int>, Service1<int>>();
            host.Open();

            ChannelFactory<IService1<int>> channel = MyEnvionment.CreateProxy<IService1<int>>();
            var service1 = channel.CreateChannel(MyEnvionment.GetEndPointUri<IService1<int>>());

            service1.GetData(
                new CompositeType() {BoolValue = true, StringValue = "Hello"}
                );

            channel.Close();
            host.Close();
        }

        [Test]
        public void Test_Traffic_Behavior()
        {
            TrafficStatistic serverTraffic = new TrafficStatistic();
            var serverConfiguration = new TrafficStatisticConfiguration(
                delegate { },
                serverTraffic.Merge);

            ServiceHost host = MyEnvionment.CreateService<IService1<int>, Service1<int>>();
            host.Description.Behaviors.Add(new TrafficBehavior(serverConfiguration));
            host.Open();

            TrafficStatistic clientTraffic = new TrafficStatistic();
            var clientConfiguration = new TrafficStatisticConfiguration(
                clientTraffic.Merge,
                delegate { });

            ChannelFactory<IService1<int>> channel = MyEnvionment.CreateProxy<IService1<int>>();
            channel.Endpoint.Behaviors.Add(new TrafficBehavior(clientConfiguration));
            var service1 = channel.CreateChannel(MyEnvionment.GetEndPointUri<IService1<int>>());

            service1.GetData(
                new CompositeType() {BoolValue = true, StringValue = "Hello"}
                );

            channel.Close();
            host.Close();

            Assert.Greater(serverTraffic.Total.Recieved, 0);
            Assert.Greater(serverTraffic.Total.Sent, 0);
            Assert.AreEqual(1, serverTraffic.Total.Requests);

            Assert.Greater(clientTraffic.Total.Recieved, 0);
            Assert.Greater(clientTraffic.Total.Sent, 0);
            Assert.AreEqual(1, clientTraffic.Total.Requests);
        }

        [Test]
        public void Test_Transparent_Delivery_Behavior()
        {
            int arrivedId = 0;
            string arrivedCulture = null;

            var tdc = new TransparentDeliveryConfiguration(
                delegate(string action, DeliveryDictionary data)
                {
                    data.Set<int>("ID", 321);
                    data.Set<string>("Culture", "en-US");
                },
                delegate(string action, DeliveryDictionary data)
                {
                    arrivedId = data.Get<int>("ID");
                    arrivedCulture = data.Get<string>("Culture");
                });

            ServiceHost host = MyEnvionment.CreateService<IService1<int>, Service1<int>>();
            host.Description.Behaviors.Add(new TransparentDeliveryBehavior(tdc));
            host.Open();

            ChannelFactory<IService1<int>> channel = MyEnvionment.CreateProxy<IService1<int>>();
            channel.Endpoint.Behaviors.Add(new TransparentDeliveryBehavior(tdc));
            var service1 = channel.CreateChannel(MyEnvionment.GetEndPointUri<IService1<int>>());

            service1.GetData(
                new CompositeType() { BoolValue = true, StringValue = "Hello" }
                );

            channel.Close();
            host.Close();

            Assert.AreEqual(321, arrivedId);
            Assert.AreEqual("en-US", arrivedCulture);
        }


    }
}